package chapter6.meine.exercises;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.BorderFactory;
import javax.swing.JApplet;
import javax.swing.JPanel;

public class PanelTwoDices extends JApplet{
	public void init() {
		setContentPane(new DicesPanel());
	}// end of init()

	
	public class DicesPanel extends JPanel implements MouseListener{
		int diceVal1, diceVal2;
		
		public DicesPanel() {
			setBackground(Color.LIGHT_GRAY); // Set up appearance of the panel
			setBorder(BorderFactory.createLineBorder(Color.BLACK, 1));
			diceVal1 = (int)(Math.random()*6) + 1;
			diceVal2 = (int)(Math.random()*6) + 1;	
			addMouseListener(this);
		}
		
		public void paintComponent(Graphics g){
			super.paintComponent(g);
			drawDice(g, diceVal1, 10, 10);
			drawDice(g, diceVal2, 60, 60);
		}
		
		public void drawDice(Graphics g, int diceVal, int x, int y) {
			g.setColor(Color.white);
			g.fillRect(x + 1, y + 1, 38, 38);
			g.setColor(Color.black);
			g.drawRect(x, y, 40, 40);
			switch (diceVal) {
			case 1:
				g.fillOval(x + 20, y + 20, 9, 9);
				break;
			case 2:
				g.fillOval(x + 3, y + 3, 9, 9);
				g.fillOval(x + 28, y + 28, 9, 9);

				break;
			case 3:
				g.fillOval(x + 3, y + 3, 9, 9);
				g.fillOval(x + 16, y + 16, 9, 9);
				g.fillOval(x + 28, y + 28, 9, 9);
				break;
			case 4:
				g.fillOval(x + 3, y + 3, 9, 9);
				g.fillOval(x + 28, y + 28, 9, 9);
				g.fillOval(x + 3, y + 28, 9, 9);
				g.fillOval(x + 28, y + 3, 9, 9);
				break;
			case 5:
				g.fillOval(x + 3, y + 3, 9, 9);
				g.fillOval(x + 28, y + 28, 9, 9);
				g.fillOval(x + 3, y + 28, 9, 9);
				g.fillOval(x + 28, y + 3, 9, 9);
				g.fillOval(x + 16, y + 16, 9, 9);
				
				break;
			case 6:
				g.fillOval(x + 3, y + 3, 9, 9);
				g.fillOval(x + 28, y + 28, 9, 9);
				g.fillOval(x + 3, y + 28, 9, 9);
				g.fillOval(x + 28, y + 3, 9, 9);
				g.fillOval(x + 3, y + 16, 9, 9);
				g.fillOval(x + 28, y + 16, 9, 9);				
				break;
			default:
				break;
			}
		}// end of drawDice()

		@Override
		public void mouseClicked(MouseEvent e) {

		}

		@Override
		public void mouseEntered(MouseEvent e) {}

		@Override
		public void mouseExited(MouseEvent e) {}

		@Override
		public void mousePressed(MouseEvent e) {
			diceVal1 = (int)(Math.random()*6) + 1;
			diceVal2 = (int)(Math.random()*6) + 1;
			repaint();
		}

		@Override
		public void mouseReleased(MouseEvent e) {}
		
	}// end of class DicesPanel
}// end of class PanelTwoDices
